package com.zd.service.impl;

import com.zd.mapper.UserInfoMapper;
import com.zd.model.UserInfo;
import com.zd.service.ILoginService;
import com.zd.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public Map<String,Object> checkLogin(UserInfo userInfo) {

        //结果Map
        Map<String,Object> resultMap=new HashMap<String,Object>();

        List<UserInfo> userInfoList = userInfoMapper.selectByPrimaryKey(userInfo);

        if(userInfoList!=null && userInfoList.size()!=0){
            //查出多个相同用户名也返回错误
            if(userInfoList.size()>1){
                resultMap.put("result",false);

            }
            else{
                UserInfo userInfoData = userInfoList.get(0);

                //判断密码是否相等
                if(!MD5Util.encode(userInfo.getPassword()).equals(userInfoData.getPassword())){
                    resultMap.put("result",false);
                }
                else{
                    resultMap.put("result",true);
                    resultMap.put("userInfo",userInfoData);
                }

            }
        }
        else{
            resultMap.put("result",false);
        }


        return resultMap;
    }
}
